﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using University.Workshop.Presenters.FeesAndFines.Register;

namespace University.Workshop.Presentation.FeesAndFines
{
    /// <summary>
    /// Interaction logic for RegisterFees.xaml
    /// </summary>
    public partial class RegisterFees : Page, IRegisterFeesView
    {
        PresenterRegisterFees pRegisterFees;
        public RegisterFees()
        {
            InitializeComponent();
            var range = new CalendarDateRange(DateTime.MinValue, DateTime.Today.AddDays(-1));
            effectiveDateDpck.BlackoutDates.Add(range);
            pRegisterFees = new PresenterRegisterFees(this);
        }

        private void RegisterFees_Loaded(object sender, RoutedEventArgs e)
        {
            App.Current.MainWindow.Title = "Registrar Tarifas";
        }

        public void RestartRegister()
        {
            this.NavigationService.Navigate(new RegisterFees());
        }

        #region Fields
        public decimal Charge
        {
            get
            {
                decimal amount;
                if (Decimal.TryParse(chargeTxt.Text, out amount))
                    return amount;
                else
                    return 0;
            }
            set
            {
                chargeTxt.Text = value.ToString();
            }
        }

        public decimal NewConnection
        {
            get
            {
                decimal amount;
                if (Decimal.TryParse(newConnectionTxt.Text, out amount))
                    return amount;
                else
                    return 0;
            }
            set
            {
                newConnectionTxt.Text = value.ToString();
            }
        }

        public decimal Reconnection
        {
            get
            {
                decimal amount;
                if (Decimal.TryParse(reconnectionTxt.Text, out amount))
                    return amount;
                else
                    return 0;
            }
            set
            {
                reconnectionTxt.Text = value.ToString();
            }
        }

        public decimal BaseRate
        {
            get
            {
                decimal amount;
                if (Decimal.TryParse(baseRateTxt.Text, out amount))
                    return amount;
                else
                    return 0;
            }
            set
            {
                baseRateTxt.Text = value.ToString();
            }
        }

        public decimal NewMember
        {
            get
            {
                decimal amount;
                if (Decimal.TryParse(newMenberTxt.Text, out amount))
                    return amount;
                else
                    return 0;
            }
            set
            {
                newMenberTxt.Text = value.ToString();
            }
        }

        public decimal MonthlyContribution
        {
            get
            {
                decimal amount;
                if (Decimal.TryParse(monthlyContributionTxt.Text, out amount))
                    return amount;
                else
                    return 0;
            }
            set
            {
                monthlyContributionTxt.Text = value.ToString();
            }
        }

        public decimal NonPayment
        {
            get
            {
                decimal amount;
                if (Decimal.TryParse(nonPaymentTxt.Text, out amount))
                    return amount;
                else
                    return 0;
            }

            set
            {
                nonPaymentTxt.Text = value.ToString();
            }
        }

        public decimal MeterViolation
        {
            get
            {
                decimal amount;
                if (Decimal.TryParse(meterViolationTxt.Text, out amount))
                    return amount;
                else
                    return 0;
            }

            set
            {
                meterViolationTxt.Text = value.ToString();
            }
        }

        public DateTime? EffectiveDate
        {
            get
            {
                return effectiveDateDpck.SelectedDate;
            }
            set
            {
                effectiveDateDpck.SelectedDate = value;
            }
        }
        #endregion

        #region Validations

        private void priceTxt_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            string temporal = ((TextBox)sender).Text + e.Text;
            foreach (char ch in e.Text)
            {
                e.Handled = !(char.IsDigit(ch) || ch.Equals('.'));
            }
        }

        private void effectiveDateDpck_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            string temporal = ((DatePicker)sender).Text + e.Text;
            foreach (char ch in e.Text)
            {
                e.Handled = !(char.IsDigit(ch) || ch.Equals('/'));
            }
        }

        private void effectiveDateDpck_PreviewLostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
        {
            try
            {
                if (((DatePicker)sender).Text != null && ((DatePicker)sender).Text != string.Empty)
                {
                    DateTime d = DateTime.Parse(((DatePicker)sender).Text);
                }
            }
            catch
            {
                ErrorMessage("Formato de fecha invalido");
            }
        }
        #endregion

        #region Buttons
        private void cancelBtn_Click(object sender, RoutedEventArgs e)
        {
            this.NavigationService.Navigate(new SearchFees());
        }

        private void registerBtn_Click(object sender, RoutedEventArgs e)
        {
            pRegisterFees.RegisterFees();
        }
        #endregion

        #region ErrorMessages
        public void SystemMessage(string MessageValue)
        {
            MessageBox.Show(App.Current.MainWindow, MessageValue, "Atencion!!!", MessageBoxButton.OK, MessageBoxImage.Information, MessageBoxResult.OK, MessageBoxOptions.RightAlign);
        }

        public void ErrorMessage(string MessageValue)
        {
            MessageBox.Show(App.Current.MainWindow, MessageValue, "Error!!!", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK, MessageBoxOptions.RightAlign);
        }
        #endregion

    }
}
